package com.example.helloworld.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import com.example.helloworld.R;

public class CustomViewBitmap extends View {

	private Bitmap cacheBitmap;
	private Canvas cacheCanvas;
	private Paint paint;
	
	public CustomViewBitmap(Context context) {
		super(context);
		paint =	new Paint();
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		createCacheBitmap(w, h);
		testDrawing();
	}
	
	private void createCacheBitmap(int w, int h) {
		cacheBitmap =	Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		cacheCanvas =	new Canvas();
		cacheCanvas.setBitmap(cacheBitmap);
	}
	
	private void testDrawing() {
		cacheCanvas.drawColor(Color.WHITE);
		
		paint.setColor(Color.RED);
		cacheCanvas.drawRect(100, 100, 200, 200, paint);
		
		Bitmap srcImg =	BitmapFactory.decodeResource(getResources(), R.drawable.waterdrop);
		cacheCanvas.drawBitmap(srcImg, 30, 30, paint);
		
		Matrix horInverseMatrix =	new Matrix();
		horInverseMatrix.setScale(-1, 1);
		Bitmap horInverseImg =	Bitmap.createBitmap(srcImg, 0, 0,
				srcImg.getWidth(), srcImg.getHeight(), horInverseMatrix, false
			);
		cacheCanvas.drawBitmap(horInverseImg, 30, 130, paint);
		
		Matrix verInverseMatrix =	new Matrix();
		verInverseMatrix.setScale(1, -1);
		Bitmap verInverseImg =	Bitmap.createBitmap(srcImg, 0, 0,
				srcImg.getWidth(), srcImg.getHeight(), verInverseMatrix, false
			);
		cacheCanvas.drawBitmap(verInverseImg, 30, 230, paint);
		
		Bitmap srcImg2 =	BitmapFactory.decodeResource(getResources(), R.drawable.face);
		paint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));
		Bitmap scaledImg =	Bitmap.createScaledBitmap(srcImg2,
				srcImg2.getWidth() * 2, srcImg2.getHeight() * 2, false);
		cacheCanvas.drawBitmap(scaledImg, 100, 230, paint);
		
		cacheCanvas.drawBitmap(srcImg2, 100, 500, null);
		
		Rect src =	new Rect(0, 0, 50, 50);
		Rect dst =	new Rect(300, 500, 400, 600);
		cacheCanvas.drawBitmap(srcImg2, src, dst, null);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (cacheBitmap != null) {
			canvas.drawBitmap(cacheBitmap, 0, 0, null);
		}
	}
}
